<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>添加物料</title>
		<link rel="stylesheet" type="text/css" href="../css/supplyProductsAddStyle.css" />
		<script type="text/javascript" src="../js/jquery.min.js" charset="utf-8"></script>
		<script src="../js/deom.js" type="text/javascript" charset="utf-8"></script>
		<meta name="viewport" content=" height=device-height,
			          width=device-width,
			          initial-scale=1.0,
			          minimum-scale=1.0,
			          maximum-scale=1.0,
			          user-scalable=no" />
		<script type="text/javascript">
			$(function() {
				var isok;
				var ip = localStorage.getItem("ip");
				var token = localStorage.getItem("token");
				$('#addbut').click(function() {
					var userId = localStorage.getItem("userId");
					var bomPicture = document.getElementById('imgs').src;
					var bomNum = document.getElementById('bomNum').value;
					if(bomNum == null) {
						alert("编码不能为空");
						isok = false;
					}
					var partsName = document.getElementById('partsName').value;
					if(partsName == null) {
						alert("名称不能为空");
						isok = false;
					}
					var priceTax = document.getElementById('priceTax').value;
					if(priceTax == null) {
						alert("单价不能为空");
						isok = false;
					}
					var bomCategory = document.getElementById('bomCategory').value;
					if(bomCategory == null) {
						alert("类别不能为空");
						isok = false;
					}
					var oneWeight = document.getElementById('oneWeight').value;
					if(oneWeight == null) {
						alert("单重不能为空");
						isok = false;
					}
					var remarks = document.getElementById('remarks').value;
					if(remarks == null) {
						alert("单位不能为空");
						isok = false;
					}
					var unit = document.getElementById('unit').value;
					if(unit == null) {
						alert("备注不能为空");
						isok = false;
					}
					$.ajax({
						type: 'post', //类型
						url: ip + 'bom/add', //请求路径
						contentType: 'application/x-www-form-urlencoded', //默认
						dataType: 'json', //返回数据类型  可以是text：字符串  可以是json：json对象
						data: {
							'bomPicture': bomPicture,
							'bomNum': bomNum,
							'userId': userId,
							'partsName': partsName,
							'priceTax': priceTax,
							'bomCategory': bomCategory,
							'oneWeight': oneWeight,
							'remarks': remarks,
							'unit': unit,
							'token': token,

						}, //数据
						success: function(result) {
							if(result.code == 200) {
								if(isok != false) {
									localStorage.removeItem('bomfiles');
									window.location.href = 'materielInfoList.html';
								}
							} else {
								alert(result.msg);
							}
						},
						error: function(data) { //若果传过来的数据为空
						}
					});
				});
				//展示层
				function showLayer(id) {
					var layer = $('#' + id),
						layerwrap = layer.find('.hw-layer-wrap');
					layer.fadeIn();
					//屏幕居中
					layerwrap.css({
						'margin-top': -layerwrap.outerHeight() / 2
					});
				}

				//隐藏层
				function hideLayer() {
					$('.hw-overlay').fadeOut();
				}

				$('.hwLayer-cancel').on('click', function() {
					hideLayer();
				});
				$('#bot').on('click', function() {
					var formData = new FormData();
					//var name = $("input").val();
					formData.append("myfiles", $("#myfiles")[0].files[0]);
					//formData.append("name", name);
					$.ajax({
						url: ip + 'product/suppeditimage',
						type: 'POST',
						data: formData,
						// 告诉jQuery不要去处理发送的数据
						processData: false,
						// 告诉jQuery不要去设置Content-Type请求头
						contentType: false,
						beforeSend: function() {
							console.log("正在进行，请稍候");
						},
						success: function(result) {
							var re = JSON.parse(result);
							document.getElementById('imgs').src = re.data;
						},
						error: function(result) {
							console.log("error");
						}
					});
					$('.hw-overlay').fadeOut();
				});
				//触发弹出层
				$('.show-layer').on('click', function() {
					var layerid = $(this).data('show-layer');
					showLayer(layerid);
				});

				//点击或者触控弹出层外的半透明遮罩层，关闭弹出层
				$('.hw-overlay').on('click', function(event) {
					if(event.target == this) {
						hideLayer();
					}
				});

				//按ESC键关闭弹出层
				$(document).keyup(function(event) {
					if(event.keyCode == 27) {
						hideLayer();
					}
				});
			});
		</script>
	</head>

	<body>
		<div id="igs" class="igclass">
			<a class="ig" href="#"><img class="imgs" id="imgs" src="http://39.108.187.126:8080/nop.png" /></a>
		</div>
		<textarea class="btn btn-info btn-lg show-layer" data-show-layer="hw-layer" role="button">添加产品图片</textarea>
		<div class="hw-overlay" id="hw-layer">
			<div class="hw-layer-wrap">
				<tr>
					<td><input id="myfiles" style="width: 100%;" name="myfiles" type="file" placeholder="请输选图片" class="easyui-validatebox span3" data-options="required:true" value=""></td>
					<td><button class="btn btn-warning hwLayer-cancel" id="bot">提交</button></td>
				</tr>
				<button class="btn btn-warning hwLayer-cancel">取 消</button>
			</div>
		</div>
		<div class="pro_much">
			<label>价&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;格:</label>
			<input type="text" id="priceTax" /><br />
			<label>单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;重:</label>
			<input type="text" id="oneWeight" /><br />
			<label>单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;位:</label>
			<input type="text" id="remarks" /><br />
		</div>
		<div class="pro_info">
			<label>物料名称:</label>
			<input type="text" id="partsName" /><br />
			<label>物料类别:</label>
			<input type="text" id="bomCategory" /><br />
			<label>物料编码:</label>
			<input type="text" id="bomNum" /><br />
			<label>物料备注:</label>
			<input type="text" id="unit" /><br />

		</div>
		<input id="addbut" class="but" type="button" value="保存" />
	</body>

</html>